package cn.com.thinvent.dispatch.web.interceptor;

import cn.com.thinvent.dispatch.entity.ModelUser;
import cn.com.thinvent.dispatch.web.utils.UserContext;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
public class MyLoginInterception implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
//        System.out.println(uri);
        // 判断URI是否属于不需要拦截的页面
        if (isCludedPage(uri)) {
            // 封装用户信息id
            String id = request.getHeader("user");
            UserContext.put(Integer.parseInt(id));
            return true;  // 拦截，继续执行后续的请求处理流程
        }
        return true;
    }
    /**
     * 判断请求的URI是否属于不需要拦截的页面
     * @param uri 请求的URI
     * @return 是否属于不需要拦截的页面
     */
    private boolean isCludedPage(String uri) {
        boolean res =
//                                uri.contains("pdfToText") ||
                                uri.contains("dataSource") ||
                                uri.contains("target") ;
        return res;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
